package com.xbts.web.controller.cloud;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xbts.common.annotation.Log;
import com.xbts.common.core.controller.BaseController;
import com.xbts.common.core.domain.AjaxResult;
import com.xbts.common.enums.BusinessType;
import com.xbts.system.domain.CnWorkOrder;
import com.xbts.system.service.ICnWorkOrderService;
import com.xbts.common.utils.poi.ExcelUtil;
import com.xbts.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * 工单Controller
 * @author xbts
 * @date 2023-08-28
 */
@Api("工单")
@RestController
@RequestMapping("/system/order")
public class CnWorkOrderController extends BaseController {

    @Autowired
    private ICnWorkOrderService cnWorkOrderService;

    /**
     * 查询工单列表
     */
    @ApiOperation("查询工单列表")
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody CnWorkOrder cnWorkOrder) {
        startPage(cnWorkOrder);
        List<CnWorkOrder> list = cnWorkOrderService.selectCnWorkOrderList(cnWorkOrder);
        return getDataTable(list);
    }

    /**
     * 导出工单列表
     */
    @ApiOperation("导出工单列表")
    @PreAuthorize("@ss.hasPermi('system:order:export')")
    @Log(title = "工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,@RequestBody CnWorkOrder cnWorkOrder) {
        List<CnWorkOrder> list = cnWorkOrderService.selectCnWorkOrderList(cnWorkOrder);
        ExcelUtil<CnWorkOrder> util = new ExcelUtil<CnWorkOrder>(CnWorkOrder.class);
        util.exportExcel(response, list, "工单数据");
    }

    /**
     * 获取工单详细信息
     */
    @ApiOperation("获取工单详细信息")
    @ApiImplicitParam(name = "orderNum", value = "工单ID", required = true, dataType = "String", paramType = "path", dataTypeClass=String.class)
    @PreAuthorize("@ss.hasPermi('system:order:query')")
    @GetMapping(value = "/{orderNum}")
    public AjaxResult getInfo(@PathVariable("orderNum") String orderNum) {
        return success(cnWorkOrderService.selectCnWorkOrderByOrderNum(orderNum));
    }

    /**
     * 新增工单
     */
    @ApiOperation("新增工单")
    @PreAuthorize("@ss.hasPermi('system:order:add')")
    @Log(title = "工单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody CnWorkOrder cnWorkOrder) {
        return toAjax(cnWorkOrderService.insertCnWorkOrder(cnWorkOrder));
    }

    /**
     * 修改工单
     */
    @ApiOperation("修改工单")
    @PreAuthorize("@ss.hasPermi('system:order:edit')")
    @Log(title = "工单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CnWorkOrder cnWorkOrder) {
        return toAjax(cnWorkOrderService.updateCnWorkOrder(cnWorkOrder));
    }

    /**
     * 删除工单
     */
    @ApiOperation("删除工单")
    @ApiImplicitParam(name = "orderNums", value = "工单ID集合", required = true, dataType = "String", paramType = "path", dataTypeClass=String.class)
    @PreAuthorize("@ss.hasPermi('system:order:remove')")
    @Log(title = "工单", businessType = BusinessType.DELETE)
	@PostMapping("/batchDelete")
    public AjaxResult batchDelete(@RequestBody List<String> orderNums) {
        return toAjax(cnWorkOrderService.deleteCnWorkOrderByOrderNums(orderNums));
    }

}
